package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.tools.Constants;

/**
 * @author lemonjesnme
 * @category friends list or all friends
 */
public class C2S2returnFriends extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:"+Constants.DBNAME);
		DB.connDB();
		super.init();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb2] whitebox2 begin");
		PrintWriter writer = response.getWriter();
		String _id = null;
		String user_id = null;
		String goto1 = null;
		String cmd = null;
		String all = null;
		while (er.hasMoreElements()) {
			String element = er.nextElement();
			/**
			 * the user_detail view my style view.
			 */
			// if (element.equals("my_style")) {
			// friends_id = request.getParameterValues("my_style")[0];
			// if (friends_id != null) {
			// writer.println("hi,i'm lisa.i stay with sky,oean,&far far away.");
			// }
			// }
			/**
			 * when the user is true, return the curret user friends ist.
			 */

			if (element.equals("cmd")) {
				cmd = request.getParameterValues("cmd")[0];
				System.out.println("[vswb2] cmd:" + cmd);
			}
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
				System.out.println("[vswb2] user_id:" + user_id);
			}
			if (element.equals("_id")) {
				_id = request.getParameterValues("_id")[0];
				System.out.println("[vswb2] _id:" + _id);
			}
			if (element.equals("goto")) {
				goto1 = request.getParameterValues("goto")[0];
				System.out.println("[vswb2] goto1:" + goto1);
			}
			if (element.equals("all")) {
				all = "all";
				System.out.println("[vswb2] all:" + all);
			}
		}

		if (user_id != null && cmd.equals("2")) {
			ResultSet rs = DB.select("Friends", "_id", "user_id", String.valueOf(user_id), "_id");
			try {
				if (rs != null) {
					StringBuffer str = new StringBuffer();
					str.append("{friends:");
					String temp1 = new String();
					Boolean first = true;
					while (rs.next()) {
						String temp = rs.getString("_id");
						if(first){
							temp1 = temp;
							str.append(temp);
							str.append(",");
							first = false;
						}
						if(temp1.equals(temp)){
							
						}else{
							str.append(temp);
							str.append(",");
							temp1 = temp;
						}
						
					}
					str.setCharAt(str.length() - 1, '}');
					writer.println(str.toString());
					System.out.println("[vswb2] friends:" + str);
				}
			} catch (SQLException e) {
				System.out.println("[vswb2] select name from friends error.");
			}
		}

		if (goto1 != null && cmd.equals("1")) {
			goto1 = request.getParameterValues("goto")[0];
			System.out.println("[vswb2] goto1:" + goto1);
			ResultSet rs = DB.select("Friends", "user_id", "_id", _id, "user_id");
			try {
				if (rs != null) {
					StringBuffer str = new StringBuffer();
					str.append("{friends:");
					String temp1 = new String();
					Boolean first = true;
					while (rs.next()) {
						String temp = rs.getString("user_id");
						if(first){
							temp1 = temp;
							str.append(temp);
							str.append(",");
							first = false;
						}
						if(temp1.equals(temp)){
							
						}else{
							str.append(temp);
							str.append(",");
							temp1 = temp;
						}
						
					}
					str.setCharAt(str.length() - 1, '}');
					writer.println(str.toString());
					System.out.println("[vswb2] friends:" + str);
				}
			} catch (SQLException e) {
				System.out.println("[vswb2] select name from friends error.");
			}
		}

		
		if((_id != null)&& cmd.equals("3")){
			ResultSet rs1 = DB.select("User", "phone_number", "_id", _id, null);
			try {
				if (rs1 != null) {
					StringBuffer str1 = new StringBuffer();
					while (rs1.next()) {
						str1.append(rs1.getString("phone_number"));
					}
					writer.println(str1.toString());
					System.out.println("[vswb2] friends name:" + str1);
				}
			}catch (Exception e1){
				System.out.println("[vswb2] select name from friends error.");
			}
		}
		
		if (all != null) {
			ResultSet rs = DB.select("select user_id from userinfo order by time asc;");
			try {
				if (rs != null) {
					StringBuffer str = new StringBuffer();
					str.append("{friends:");
					while (rs.next()) {
						str.append(rs.getString("user_id"));
						str.append(",");
					}
					str.setCharAt(str.length() - 1, '}');
					writer.println(str.toString());
					System.out.println("[vswb2] friends:" + str);
				}
			} catch (SQLException e) {
				System.out.println("[vswb2] select id from friends error.");
			}
		}
		
	}

}
